package com.example.nexttome_mobile;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

@SuppressLint("NewApi")
public class Map extends Activity{
	
	private LocationManager locMan;
	private Marker userMarker;
	
	private GoogleMap map;
	static final LatLng ROSARIO = new LatLng(-32.9511111, -60.6663889);
	static final LatLng KIEL = new LatLng(53.551, 9.993);
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map);
				
		map = ((MapFragment) getFragmentManager().findFragmentById(R.id.the_map)).getMap();
		if (map!=null){
			Bundle bundle = getIntent().getExtras();
			
		    LatLng point1 = new LatLng(Double.parseDouble(bundle.getString("latitud0")), Double.parseDouble(bundle.getString("longitud0")));
		    Marker p1 = map.addMarker(new MarkerOptions()
	      	  .position(point1)
	          .title("Primero"));
		    
		    LatLng point2 = new LatLng(Double.parseDouble(bundle.getString("latitud1")), Double.parseDouble(bundle.getString("longitud1")));
		    Marker p2 = map.addMarker(new MarkerOptions()
	      	  .position(point2)
	          .title("Segundo"));
		    
		    LatLng point3 = new LatLng(Double.parseDouble(bundle.getString("latitud2")), Double.parseDouble(bundle.getString("longitud2")));
		    Marker p3 = map.addMarker(new MarkerOptions()
	      	  .position(point3)
	          .title("Tercero"));
		    
		    Marker kiel = map.addMarker(new MarkerOptions()
		          .position(KIEL)
		          .title("Kiel")
		          .snippet("Kiel is cool")
		          .icon(BitmapDescriptorFactory
		              .fromResource(R.drawable.ic_launcher)));
			
			// Move the camera instantly to Rosario with a zoom of 17.
			map.moveCamera(CameraUpdateFactory.newLatLngZoom(point1, 17));
			// Zoom in, animating the camera.
			map.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
		}else{
			Marker hamburg = map.addMarker(new MarkerOptions()
	      	  .position(ROSARIO)
	          .title("Hamburg"));
			// Move the camera instantly to Rosario with a zoom of 17.
			map.moveCamera(CameraUpdateFactory.newLatLngZoom(ROSARIO, 17));
		}
}
	
	
	private void updatePlaces(){
		LatLng lastLatLng;
		locMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		Location lastLoc = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
		Double lat = lastLoc.getLatitude();
		Double lng = lastLoc.getLongitude();
		if(lat != null && lng != null){
		    lastLatLng = new LatLng(lat, lng); 
		}else{
			lastLatLng = new LatLng(-32.9511111, -60.6663889); 
		}
				
		if(userMarker!=null) userMarker.remove();
		
		userMarker = map.addMarker(new MarkerOptions()
	    	.position(lastLatLng)
	    	.title("You are here")
	    	.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))
	    	.snippet("Your last recorded location"));
		
		map.animateCamera(CameraUpdateFactory.newLatLng(lastLatLng), 3000, null);
	}
	
	
	@Override
	public void onBackPressed() {
		Intent i = new Intent(this, MainActivity.class);
		startActivity(i);
		finish();
	}
	
	

}
